#include "main.h"
//音调
#define L1 1
#define L1_ 2
#define L2 3
#define L2_ 4
#define L3 5
#define L4 6
#define L4_ 7
#define L5 8
#define L5_ 9
#define L6 10
#define L6_ 11
#define L7 12
#define M1 13
#define M1_ 14
#define M2 15
#define M2_ 16
#define M3 17
#define M4 18
#define M4_ 19
#define M5 20
#define M5_ 21
#define M6 22
#define M6_ 23
#define M7 24
#define H1 25
#define H1_ 26
#define H2 27
#define H2_ 28
#define H3 29
#define H4 30
#define H4_ 31
#define H5 32
#define H5_ 33
#define H6 34
#define H6_ 35
#define H7 36
#define R 37

//16分音符的时间
#define Time_16 125

sbit BEEP=P2^5;

// 音调表，依次是低，中，高 1~7
unsigned int code Pitch_Table[]={
    63628,63731,63835,63928,64021,64103,64185,64260,64331,64400,64463,64524,
    64580,64633,64684,64732,64777,64820,64860,64898,64934,64968,65000,65030,
    65058,65085,65110,65134,65157,65178,65198,65217,65235,65252,65268,65283,0};


// 里面放谱子，分别轮放音调和音符，16分音符为1，八分音符为2，四分音符为4，两分音符为8，一分音符为16
//例：H1,4,
//    H1_,4,
//加在0xff前面，表示结束
unsigned char code Sheet_Music[] = {
M1,4,
M1,4,
M5,4,
M5,4,
M6,4,
M6,4,
M5,8,
M4,4,
M4,4,
M3,4,
M3,4,
M2,4,
M2,4,
M1,8,
M5,4,
M5,4,
M4,4,
M4,4,
M3,4,
M3,4,
M2,8,
M5,4,
M5,4,
M4,4,
M4,4,
M3,4,
M3,4,
M2,8,
M1,4,
M1,4,
M5,4,
M5,4,
M6,4,
M6,4,
M5,8,
M4,4,
M4,4,
M3,4,
M3,4,
M2,4,
M2,4,
M1,8,
0xff};

// unsigned char code Sheet_Music[] = {
// M5, 4, M5, 4, M6, 4, M5, 4, M1, 4, M7, 8,
// R, 4,  // 休止符，占一个四分音符
// M5, 4, M5, 4, M6, 4, M5, 4, M2, 4, M1, 8,
// M5, 4, M5, 4, H5, 4, M3, 4, M1, 4, M7, 4, M6, 4,
// M4, 4, M4, 4, M3, 4, M1, 4, M2, 8, M5, 4, M5, 4,
// H6, 4, H6, 4, H5, 4, H3, 4, H1, 4, M7, 8,
// R, 8,  // 休止符，占两拍
// M5, 4, M5, 4, M6, 4, M5, 4, M2, 4, M1, 8,
// M5, 4, M5, 4, H5, 4, M3, 4, M1, 4, M7, 4, M6, 4,
// M4, 4, M4, 4, M3, 4, M1, 4, M2, 8, M5, 4, M5, 4,
// M5, 4, M5, 4, M6, 4, M5, 4, H1, 4, M7, 8,
// M5, 4, M5, 4, M6, 4, M5, 4, H2, 4, H1, 8,
// H6, 4, H6, 4, H5, 4, H3, 4, H1, 4, M7, 8,
// M1, 4, M5, 4, M5, 4, M6, 4, M5, 4, M3, 8,
// R, 4,  // 休止符，占一个四分音符
// 0xff    // 结束符
// };

//计数器
unsigned char Counter=0;
//音调存储器
unsigned char Pitch=0;


void main()
{
    Timer0Init();

    while (1)
    {
        Pitch=Sheet_Music[Counter];
        Counter++;
        Delay(Time_16*Sheet_Music[Counter]);
        EA=0;
        Counter++;
        Delay(200);
        Pitch=Sheet_Music[Counter];
        EA=1;
        if(Sheet_Music[Counter]==0xff)
        {
            EA=0;
            while(1)
            {

            }
        }
    }
}

void Timer_Cut() interrupt 1
{
	TL0 = Pitch_Table[Pitch]%256;		//设置定时初值
	TH0 = Pitch_Table[Pitch]/256;	//设置定时初值
    if(Pitch==R)
    {
        return;
    }	
    BEEP=~BEEP;
	
}